/*
 * @lc app=leetcode.cn id=884 lang=java
 *
 * [884] 两句话中的不常见单词
 */

// @lc code=start
class Solution
{
  public String[] uncommonFromSentences(String s1, String s2)
  {
    String[] words1 = s1.split("\\s+"), words2 = s2.split("\\s+");
    Map<String, Integer> wordCnt = new HashMap<>();
    for (String word : words1)
      wordCnt.put(word, wordCnt.getOrDefault(word, 0) + 1);
    for (String word : words2)
      wordCnt.put(word, wordCnt.getOrDefault(word, 0) + 1);
    List<Map.Entry<String, Integer>> entry = new ArrayList<>(wordCnt.entrySet());
    Collections.sort(entry, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
    List<String> uncom = new ArrayList<>();
    for (Map.Entry<String, Integer> e : entry) {
      if (e.getValue() == 1)
        uncom.add(e.getKey());
      else
        break;
    }
    String[] ans = new String[uncom.size()];
    for (int i = 0; i < uncom.size(); ++i)
      ans[i] = uncom.get(i);
    return ans;
  }
}
// @lc code=end
